<?php 
	class Lives extends CI_Model{

	 	function __construct(){
        	parent::__construct();
    	}
    	/**
    	 * 获取最后一条直播
    	 * @return [type] [description]
    	 */
    	function get_last_zhibo(){
    		$sql = "select c.*,t.teacher_name,t.school_name from edu_course as c 
				left join edu_o2o_teachers as t on(c.teacher_id = t.id) 
				where c.school_id= $this->school_id and c.type = 2 and c.status = 1
				order by c.id desc limit 1";
			return $this->db->query($sql)->row_array();
    	}
    	/**
    	 * 即将直播
    	 * @param  integer $limit [description]
    	 * @return [type]         [description]
    	 */
		function get_jijiangzhibo_list($limit = 8){
			$time = time();
			$where = "where 1=1 and c.school_id=$this->school_id and c.start_time > 0 and c.start_time > $time and $time < c.end_time and c.type = 2 and c.status = 1";
			$sql = "select c.*,t.teacher_name,t.school_name from {$this->db->dbprefix}course as c left join {$this->db->dbprefix}o2o_teachers as t on(c.teacher_id = t.id) $where order by id desc limit 0,$limit";
			$result = $this->db->query($sql)->result_array();
			return $result;
		}

		/**
		 * 
		 * 获取进行中直播
		 * @return [type] [description]
		 */
		function get_live_starting(){
			$sql = "select c.*,t.teacher_name,t.school_name from {$this->db->dbprefix}course as c left join {$this->db->dbprefix}o2o_teachers as t on(c.teacher_id = t.id) 
				where c.school_id= $this->school_id 
				and ( (c.start_time >0 and unix_timestamp(now()) > c.start_time ) and (c.end_time > 0 and unix_timestamp(now()) < c.end_time) ) 
				and c.type = 2 and c.status = 1
				order by c.end_time asc";
			$result = $this->db->query($sql)->result_array();
			return $result;
		}


		function get_course_info($id){
			$sql = "SELECT c.*,ot.teacher_name,ot.school_name,ot.header_img,ot.intro,os.xiaozhang,os.link_phone,os.school_logo,os.id as sid,u.stream_id from {$this->db->dbprefix}course as c left join {$this->db->dbprefix}o2o_teachers as ot on(c.teacher_id = ot.id) left join {$this->db->dbprefix}o2o_school as os on(ot.school_id = os.id) left join {$this->db->dbprefix}user as u on(u.teacher_id = ot.id) where c.id = ".$id;
			$result = $this->db->query($sql)->row_array();
			return $result;
		}

		/**
		 * 直播结束
		 * @param  integer $limit [description]
		 * @return [type]         [description]
		 */
		function get_lishi_gzhibo_list($limit = 6){
			$time = time();
			$where = "where 1=1 and c.school_id=$this->school_id and c.end_time > 0 and $time > c.end_time and c.type = 2 and c.status = 1";
			$sql = "select c.*,t.teacher_name,t.school_name from {$this->db->dbprefix}course as c left join {$this->db->dbprefix}o2o_teachers as t on(c.teacher_id = t.id) $where order by c.id desc limit 0,$limit";
			$result = $this->db->query($sql)->result_array();
			return $result;
		}


		function get_live_status($id){
			$time = time();
			$sql = "SELECT * from {$this->db->dbprefix}course where id = $id and $time > end_time and type = 2 and status = 1";
			$result = $this->db->query($sql)->row_array();
			return $result;
		}

		function check_collect_status($uid,$course_id,$type){
			$sql = "SELECT id from {$this->db->dbprefix}user_collect where uid = $uid and top_id = $course_id and type = $type";
			$result = $this->db->query($sql)->row_array();
			return $result;
		}
		
	}